package com.llgz.hotelmanageapi.exception;


import com.auth0.jwt.exceptions.TokenExpiredException;
import org.apache.shiro.authc.UnknownAccountException;
import com.llgz.hotelmanageapi.common.CommonResult;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Bernie_fang
 * @Description: spring boot 异常处理：主要处理shiro注解抛出的异常,统一返回异常结果（json数据）给前端验证
 * @create 2021/7/23 17:18
 **/
@RestControllerAdvice //异常处理的注解
public class BaseExceptionHandler {

    /**
     * 处理未授权的异常
     */
    @ExceptionHandler(value = AuthorizationException.class)
    public CommonResult returnErrorMsg(HttpServletRequest request, HttpServletResponse response, AuthorizationException exception) {
        return CommonResult.errorResponse("您没有权限", 403);
    }

    /**
     * 处理未登录的异常
     */
    @ExceptionHandler(value = UnauthenticatedException.class)
    public CommonResult returnUnLoginErrorMsg(HttpServletRequest request, HttpServletResponse response, UnauthenticatedException exception) {
        return CommonResult.errorResponse("您还未登录,或您的身份信息已失效", 401);
    }

    /**
     * 处理运行时异常
     */
    @ExceptionHandler(value = Exception.class)
    public CommonResult returnRunningException(Exception e) {
        System.out.println(e.getMessage());
        return CommonResult.errorResponse(e.getMessage());
    }


}

